
package com.dps.omc.action;

import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.common.utils.*;
import com.dps.omc.service.IJobReportDateService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.omc.model.JobReportDate;

@Controller
public class JobReportDateAction extends AbstractAdminController<IJobReportDateService>{

    @Autowired
    private IJobReportDateService jobReportDateService;


    @RequestMapping(value="/jobreportdate/query")
    public String query(Date queryStart, Date queryEnd, JobReportDate jobReportDate, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
//        Map<String, Object> conditions = getQueryMap(jobReportDate);
//        conditions.put("reportDateStart",reportDateStart);
//        conditions.put("reportDateEnd",reportDateEnd);
//        BizData4Page<JobReportDate> pageCtx = doPage(model, conditions, pageParam);
        if(queryStart==null)
        {
            queryStart = DateUtils.getSpecifiedDayBefore(DateUtils.getNowDate(),31);
        }
        if(queryEnd==null)
        {
            queryEnd = DateUtils.getSpecifiedDayBefore(DateUtils.getNowDate(),1);
        }

        model.addAttribute("queryStart",queryStart);
        model.addAttribute("queryEnd",queryEnd);
//        model.addAttribute("jobReportDate", jobReportDate);
        return "/module/jobreportdate/query";
    }

    @RequestMapping(value="/jobreportdate/repdata")
    public String repData(Date queryStart,Date queryEnd,ModelMap model,HttpServletRequest request,HttpServletResponse response) throws ParseException {

        if(queryStart==null)
        {
            queryStart = DateUtils.getSpecifiedDayBefore(DateUtils.getNowDate(),31);
        }
        if(queryEnd==null)
        {
            queryEnd = DateUtils.getSpecifiedDayBefore(DateUtils.getNowDate(),1);
        }
        List<JobReportDate> datas = getData(queryStart,queryEnd);
        model.addAttribute("datas",datas);
        return "/module/jobreportdate/repdata";
    }

    private List<JobReportDate>  getData(Date queryStart,Date queryEnd) throws ParseException {
        List<JobReportDate> data = jobReportDateService.queryByReportDate(queryStart,queryEnd);
//        for(JobReportDate a:data)
//        {
//            System.out.println(DateUtils.formatDatetime(a.getReportDate()));
//        }
        return data;
    }

    @RequestMapping(value="/jobreportdate/list")
    public String list(Date queryStart,Date queryEnd,JobReportDate jobReportDate,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(jobReportDate);
        conditions.put("queryStart",queryStart);
        conditions.put("queryEnd",queryEnd);
    	BizData4Page<JobReportDate> pageCtx = doPage(model, conditions, pageParam);
        model.addAttribute("queryStart",queryStart);
        model.addAttribute("queryEnd",queryEnd);
    	model.addAttribute("jobReportDate", jobReportDate);
    	return "/module/jobreportdate/list";
    }    
    
    @RequestMapping(value="/jobreportdate/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("jobReportDate", new JobReportDate());
        return "module/jobreportdate/edit";
    }    
    
    @RequestMapping(value="/jobreportdate/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	JobReportDate jobReportDate = jobReportDateService.fetch(id);
    	model.addAttribute("jobReportDate", jobReportDate);
        return "module/jobreportdate/edit";
    }    
    
    @RequestMapping(value="/jobreportdate/save")
    @ResponseBody
    public String save(HttpServletRequest request,JobReportDate jobReportDate){
		boolean isAddNew = StringUtils.isBlank(jobReportDate.getId())?true:false;
		if(isAddNew)
		{
			jobReportDate.setId(IdGenerator.createNewId());
			jobReportDateService.add(jobReportDate);
		}
		else
		{
			jobReportDateService.update(jobReportDate);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/jobreportdate/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	jobReportDateService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }
	
    protected Map getQueryMap(JobReportDate jobReportDate)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("jobSucc", jobReportDate.getJobSucc());		
		conditions.put("jobFail", jobReportDate.getJobFail());		
		conditions.put("reportDate", jobReportDate.getReportDate());		
		conditions.put("createDate", jobReportDate.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IJobReportDateService getMainService() {
        return jobReportDateService;
    }

    @Override
    protected String getMainObjName() {
        return "jobreportdate";
    }

    @Override
    protected String getViewTitle() {
        return "jobreportdate";
    }
}
